package com.richapp.launcher.database.system.dao;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.stmt.UpdateBuilder;
import com.richapp.launcher.database.system.SystemBaseDao;
import com.richapp.launcher.database.system.mode.RoomModel;

import android.content.Context;
import android.util.Log;

public class RoomModelDao extends SystemBaseDao<RoomModel>{

	public RoomModelDao(Context context) {
		super(context,RoomModel.class);
	}

	public List<RoomModel> getUserRoomList(String userName){
		List<RoomModel> userRoomList = null;
		try {
			userRoomList = gettOp().queryBuilder().where().eq("userName", userName).query();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return userRoomList;
	}
	
	public List<RoomModel> getRoomList(){
		List<RoomModel> userRoomList = null;
		try {
			userRoomList = gettOp().queryBuilder().query();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return userRoomList;
	}
	
	public RoomModel getByName(String roomName){
		RoomModel rm = null;
		try {
			rm =  gettOp().queryBuilder().where().eq("roomName", roomName).queryForFirst();
		} catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return rm;
	}
	
	public void updateAllUnbindRoomUserInfo(String userName){
		try {
			UpdateBuilder<RoomModel, Integer> ub = gettOp().updateBuilder();
			ub.updateColumnValue("userName", userName);
			ub.where().isNull("userName");
			ub.update();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
	}
	
}
